package cn.itcast.blog.web.demo;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.itcast.blog.domain.AjaxResponse;
import cn.itcast.blog.domain.Post;
import cn.itcast.blog.service.PostService;

/**
 * 处理查询所有文章的请求，请求拦截使用注解
 * @author 16604
 *
 */
@WebServlet(urlPatterns="/post_find")
public class PostFindServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		PostService postService = new PostService();
		
		List<Post> findByAll = postService.findByAllWithCategory();
		
		AjaxResponse<Post> ajaxPostResponse = new AjaxResponse<>();
		
		ajaxPostResponse.setCode(0);
		ajaxPostResponse.setCount(100);
		ajaxPostResponse.setMsg("");
		ajaxPostResponse.setData(findByAll);
		
		String string = JSON.toJSONString(ajaxPostResponse);
		
		resp.setContentType("application/json;charset=utf-8");
		resp.getWriter().write(string);
		
	}
	
}
